package com.example.mall.common.validator;

import com.example.mall.common.annotations.IntegerRange;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class IntegerConstraintValidator implements ConstraintValidator<IntegerRange, Integer> {

    private static final Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(IntegerRange constraintAnnotation) {
        Arrays.stream(constraintAnnotation.range()).forEach(set::add);
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
